package com.gec.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.system.*;

import com.gec.model.vo.ITripSightQueryVo;

import com.gec.system.common.Result;
//import com.gec.system.mapper.ITripSightsCityAreaMapper;
import com.gec.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 景点表 前端控制器
 * </p>
 *
 * @author dafei
 * @since 2024-01-22
 */
@RestController
@RequestMapping("/system/itrSights")
@Api(tags = "景点管理")
@CrossOrigin
public class ITripSightsController {

    @Autowired
    private ITripSightsService iTripSightsService;

    @Autowired
    private ITripAreaService iTripAreaService;
    @Autowired
    private ITripCityService iTripCityService;

    @Autowired
    private ITripImageService iTripImageService;
//    @Autowired
//    ITripSightsCityAreaMapper iTripSightsCityAreaMapper;

    @Autowired
    private ITripSightsOrderService iTripSightsOrderService;



    //    用户选择要去旅游的景点，生成对应的景点订单表 传用户id和景点id
    @ApiOperation("用户选择景点")
    @PostMapping("/chooseSights")
    public Result chooseItinerary(
            Long userId,
            Long id
    ) {
        ITripSights sights = iTripSightsService.getById(id);
//       用户选择好景点后，生成景点订单表
        ITripSightsOrder it = new ITripSightsOrder();
        it.setSightsId(id);
        it.setUserId(userId);
        it.setPrice(sights.getPrice());
        iTripSightsOrderService.save(it);

        return Result.ok();

    }


    @ApiOperation("根据id去移除一个景点")
    @DeleteMapping("/removeSights/{id}")
    public Result removeSights(@PathVariable Long id)
    {
        boolean b = this.iTripSightsService.removeById(id);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }

    @ApiOperation("uni-app 获取全部景点")
    @GetMapping("/findAll")
    public Result findAll(){
        List<ITripSights> list = this.iTripSightsService.selectPage();
        return Result.ok(list);
    }

    @ApiOperation("uni-app 获取热门景点")
    @GetMapping("/findIsGoodAll")
    public Result findIsGoodAll(){
        List<ITripSights> list = this.iTripSightsService.getByIsGood();
        return Result.ok(list);
    }

    // 分页和条件查询
    @GetMapping("/{page}/{limit}")
    public Result findSightsByPageQuery(@PathVariable Long page,
                                          @PathVariable Long limit,
                                        ITripSightQueryVo iTripSightQueryVo)
    {
        //1.创建分页对象
        IPage<ITripSights> p1 = new Page<>(page,limit);
        //2.调用方法
        p1 =    this.iTripSightsService.selectPage(p1,iTripSightQueryVo);
        //3.返回
        return Result.ok(p1);
    }

    //    添加景点前，查询所有区域
    @PostMapping("/findArea")
    public Result findArea(){
        List<ITripArea> list =iTripAreaService.list();
        return Result.ok(list);
    }
    //添加/修改景点前，根据区域查找旗下的所有城市
    @PostMapping("/findCityByAreaId/{areaId}")
    public Result findCityByAreaId(@PathVariable Long areaId){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("areaId", areaId);
        List list = iTripCityService.list(wrapper);
        return Result.ok(list);
    }

    @GetMapping("/findSightsByCityId/{cityId}")
    public Result findAreaId(@PathVariable Long cityId)
    {
        List<ITripSights> iTripCity = this.iTripSightsService.selectByCityId(cityId);
        return Result.ok(iTripCity);
    }

    // 添加景点
    @PostMapping("/addSight")
    public Result addSight(
            @RequestBody ITripSights iTripSights) {
//        添加编号唯一性判断

        ITripSights it = iTripSightsService.selectByName(iTripSights.getSightsName());
        if (it != null) {
            return Result.fail("name重复");
        }
        boolean res = this.iTripSightsService.save(iTripSights);
        ITripImage image = new ITripImage();
        image.setImage(iTripSights.getImage());
        image.setType("2");
        image.setUrl(iTripSights.getUrl());
        image.setTargetId(iTripSights.getId());
        iTripImageService.saveOrUpdate(image);

        if (res) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
    // 修改
    //1.根据id 去得到当前分类
    @GetMapping("/findSightsById/{id}")
    public Result findSightsById(@PathVariable Long id)
    {
        ITripSights iTripSights = this.iTripSightsService.getById(id);
        return Result.ok(iTripSights);
    }
    // 实现修改
    @PostMapping("/updateSights")
    public Result updateCategory(@RequestBody ITripSights iTripSights)
    {
        boolean b = this.iTripSightsService.updateById(iTripSights);
        ITripImage image = new ITripImage();
        image.setImage(iTripSights.getImage());
        image.setType("2");
        image.setUrl(iTripSights.getUrl());
        image.setTargetId(iTripSights.getId());
        Long id = iTripImageService.getId(2,iTripSights.getId());
        image.setId(id);
        iTripImageService.updateById(image);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    // 更改景点状态
    @ApiOperation("更改用户状态")
    @GetMapping("/updateStatus/{id}/{isActivated}")
    public Result updateStatus(@PathVariable Long id,
                               @PathVariable Integer isActivated)
    {
        this.iTripSightsService.updateActivated(id,isActivated);
        return Result.ok();
    }
    // 批量删除
    @DeleteMapping("/removeSightsByIds")
    public Result removeSightsByIds(@RequestBody List<Long> ids)
    {
        boolean b = this.iTripSightsService.removeByIds(ids);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }


}

